package horncomb.bpm.process.instance;

import horncomb.bpm.BpmContextHolder;
import horncomb.bpm.process.core.ExecutionContext;
import horncomb.bpm.process.core.ProcessDefinition;
import horncomb.bpm.process.core.ProcessInstance;
import horncomb.bpm.process.core.StateDefinition;
import horncomb.bpm.process.core.Token;
import horncomb.bpm.process.core.Transition;
import horncomb.bpm.process.core.entity.ProcessInstanceEntity;

/**
 * 简单状态描述
 * <P>
 * 仅描述单一状态，本单状态无父状态与子状态
 * @author Daniel
 */
public class DefaultProcessInstance extends DefaultNestedToken implements
		ProcessInstance, ProcessInstanceEntity {
	private static final long serialVersionUID = 9166999056584484801L;

	public DefaultProcessInstance() {
		super();
	}

	public DefaultProcessInstance(String processDefinitionId,
			ProcessInstanceEntity entity) {
		super(entity);
		this.getEntity().setProcessDefinitionId(processDefinitionId);
	}

	public DefaultProcessInstance(String processDefinitionId,
			ProcessInstanceEntity entity, String parentTokenId) {
		super(entity, parentTokenId);
		this.getEntity().setProcessDefinitionId(processDefinitionId);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ProcessInstanceEntity getEntity() {
		return (ProcessInstanceEntity) super.getEntity();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ProcessInstance getProcessInstance() {
		return this;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getProcessInstanceId() {
		return this.getId();
	}

	/**
	 * {@inheritDoc}
	 */
	public ProcessDefinition getProcessDefinition() {
		return BpmContextHolder.getContext().getProcessManagementService()
				.findProcessDefinition(
						this.getEntity().getProcessDefinitionId());
	}

	/**
	 * {@inheritDoc}
	 */
	public void start(Object contextModel) {
		ExecutionContext context = new DefaultExcutionContext(this,
				contextModel);
		this.getProcessDefinition().getStartState().enter(context);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public StateDefinition getState() {
		return super.getState();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setState(StateDefinition state) {
		super.setState(state);
	}

	/**
	 * {@inheritDoc}
	 */
	public void performTransition(Transition transition, Object contextModel) {
		super.performTransition(transition, contextModel);
	}

	/**
	 * @return the token
	 */
	public Token getToken() {
		return this;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getProcessDefinitionId() {
		return this.getEntity().getProcessDefinitionId();
	}

	/**
	 * {@inheritDoc}
	 */
	public void setProcessDefinitionId(String processDefinitionId) {
		this.getEntity().setProcessDefinitionId(processDefinitionId);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getTokenId() {
		return this.getEntity().getId();
	}

	/**
	 * {@inheritDoc}
	 */
	public void setTokenId(String tokenId) {
		this.getEntity().setTokenId(tokenId);
	}
}
